package com.yuexunit.operationman.nbsy.module.OperationInventory.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.InventoryBean;
import com.yuexunit.operationman.nbsy.widget.ToastDialog;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by chenss on 2017/8/15 0015.
 */

public class EditInventoryAdapter extends KJAdapter<InventoryBean.ObjectEntity.CheckItemListEntity> implements Filterable {
    private PrescriptionFilter filter;
    ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> list = new ArrayList<>();
    ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> list_filter = new ArrayList<>();
    String contains="";

    public EditInventoryAdapter(AbsListView view, Collection<InventoryBean.ObjectEntity.CheckItemListEntity> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
        list = (ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>)((ArrayList)mDatas).clone();
    }

    public ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> getGoodsValue() {
        return list;
    }

    @Override
    public void convert(AdapterHolder helper, final InventoryBean.ObjectEntity.CheckItemListEntity item, boolean isScrolling, final int position) {
        helper.setText(R.id.name,item.getItemName() + "(" + item.getItemCode() + ")");
        final ImageView pic;
        pic = helper.getView(R.id.pic);
        final EditText num = helper.getView(R.id.number);
        pic.setImageResource(R.drawable.icon_inventory_subtract);
        num.setVisibility(View.GONE);

        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ToastDialog toastDialog = new ToastDialog(mCxt);
                toastDialog.builder();
                toastDialog.setTxt("是否删除物品\n"+item.getItemName()+"\n"+item.getItemCode());
                toastDialog.setCallBack(new ToastDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        list.remove(item);
                        getFilter().filter(contains);
                        notifyDataSetChanged();
                        toastDialog.dismiss();
                    }
                });
                toastDialog.show();
            }
        });
    }

    @Override
    public Filter getFilter() {
        if(filter == null){
            filter = new PrescriptionFilter();
        }
        return filter;
    }

    class PrescriptionFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            contains = constraint.toString();
            FilterResults results = new FilterResults();
            if(constraint == null || constraint.length() == 0){
                results.values = list;
                results.count = list.size();
            }else{
                list_filter.clear();
                for (InventoryBean.ObjectEntity.CheckItemListEntity p : list){
                    if(p.getItemName().contains(constraint.toString())||p.getItemCode().contains(constraint.toString())){
                        list_filter.add(p);
                    }
                }
                results.values = list_filter;
                results.count = list_filter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            refresh((Collection<InventoryBean.ObjectEntity.CheckItemListEntity>) results.values);
        }
    }
}
